#include "BaseMainWindow.h"
#include "Config.h"
#include "utils_qt.h"
#include "ZoomButton.h"
#include "DraggableLabel.h"
using namespace Config; // 引入命名空间

#include <QGraphicsDropShadowEffect>
#include <QPropertyAnimation>
#include <QGridLayout>
#include <iostream>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QWidget>
#include <QFont>
#include <QLabel>
#include <QDebug>
#include <string>

BaseMainWindow::BaseMainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // 设置窗口标志
    setWindowFlags(Qt::FramelessWindowHint);
    // 设置透明背景
    setAttribute(Qt::WA_TranslucentBackground);

    this->resize(Window_w, Window_h);
    this->SetUp();
    this->SetDay();
}

void BaseMainWindow::SetUI()
{
    // 背景生成
    Background = new QLabel("", this);
    BackgroundShadow = GetShadow();
    Background->setGraphicsEffect(BackgroundShadow); // 将阴影效果应用到 QLabel

    ShowBox = new QLabel("", this);

    // 顶部控件加入
    vLayout = new QVBoxLayout(this->Background); // 创建一个垂直布局
    vLayout->setContentsMargins(10, 5, 10, 10);  // 设置边距为0

    hLayout = new QHBoxLayout();             // 创建一个水平布局
    hLayout->setContentsMargins(0, 0, 0, 0); // 设置边距为0
    WindowTitle = new DraggableLabel(this);
    WindowTitle->setText(QString::fromStdString(WindowTitleText));

    SetTextQLable(*WindowTitle, Font, FontSize + 11);
    // WindowTitile->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

    MiniButton = new QPushButton("-", this);  // 创建最小化按钮
    CloseButton = new QPushButton("x", this); // 创建关闭按钮
    MiniButton->setMaximumSize(WindowsButtonHeight, WindowsButtonHeight);
    CloseButton->setMaximumSize(WindowsButtonHeight, WindowsButtonHeight);
    MiniButton->setFont(QFont("Verdana", FontSize + 16));  // 设置字体
    CloseButton->setFont(QFont("Verdana", FontSize + 16)); // 设置字体

    // 将控件添加到水平布局中
    hLayout->addWidget(WindowTitle);
    hLayout->addWidget(MiniButton);
    hLayout->addWidget(CloseButton);

    // 将水平布局添加到垂直布局中
    vLayout->addLayout(hLayout);
    vLayout->addWidget(ShowBox);
    this->Background->setLayout(vLayout);

    // 缩放按钮加入
    LeftZoom = new ZoomButton(this, "L", true, false);
    RightZoom = new ZoomButton(this, "R", true, false);
    LeftDownZoom = new ZoomButton(this, "LD", true, true);
    DownZoom = new ZoomButton(this, "D", false, true);
    RightDownZoom = new ZoomButton(this, "RD", true, true);
}

void BaseMainWindow::SetUIFun()
{
    // 连接信号到槽
    connect(LeftZoom, &ZoomButton::sizeChanged, this, &BaseMainWindow::MyZoom);
    connect(RightZoom, &ZoomButton::sizeChanged, this, &BaseMainWindow::MyZoom);
    connect(LeftDownZoom, &ZoomButton::sizeChanged, this, &BaseMainWindow::MyZoom);
    connect(DownZoom, &ZoomButton::sizeChanged, this, &BaseMainWindow::MyZoom);
    connect(RightDownZoom, &ZoomButton::sizeChanged, this, &BaseMainWindow::MyZoom);

    // 移动拖放
    connect(WindowTitle, &DraggableLabel::mouseEntered, this, &BaseMainWindow::ShadowBig);
    connect(WindowTitle, &DraggableLabel::mouseLeft, this, &BaseMainWindow::ShadowSmall);

    connect(MiniButton, &QPushButton::clicked, this, &BaseMainWindow::showMinimized);
    connect(CloseButton, &QPushButton::clicked, this, &BaseMainWindow::close);
}

void BaseMainWindow::SetUILocation()
{
    int w = this->width(), h = this->height();
    LeftZoom->setGeometry(Window_Spare + 0, Window_Spare + 0, ZoomButtonLength, h - ZoomButtonLength - 2 * Window_Spare);

    Background->setGeometry(Window_Spare + ZoomButtonLength + ZoomButtonPadding,
                            Window_Spare + 0,
                            w - 2 * ZoomButtonLength - 2 * ZoomButtonPadding - 2 * Window_Spare,
                            h - ZoomButtonLength - ZoomButtonPadding - 2 * Window_Spare);

    RightZoom->setGeometry(w - ZoomButtonLength - Window_Spare, Window_Spare + 0, ZoomButtonLength, h - ZoomButtonLength - 2 * Window_Spare);

    LeftDownZoom->setGeometry(Window_Spare + 0, h - ZoomButtonLength - Window_Spare, ZoomButtonLength, ZoomButtonLength);

    DownZoom->setGeometry(ZoomButtonLength + Window_Spare, h - ZoomButtonLength - Window_Spare, w - 2 * ZoomButtonLength - 2 * Window_Spare, ZoomButtonLength);

    RightDownZoom->setGeometry(w - ZoomButtonLength - Window_Spare, h - ZoomButtonLength - Window_Spare, ZoomButtonLength, ZoomButtonLength);
}

void BaseMainWindow::MyZoom(std::string &name, int w, int h)
{
    // std::cout << "resize" << w << " " << h << std::endl;

    this->SetUILocation();

    // 如果在左边，那就需要考虑移动界面
    if (name[0] == 'L')
    {
        this->move(this->x() + w, this->y());
        w *= -1;
    }

    w += this->width();
    h += this->height();

    // 不能太小
    if (w > Window_min_w + 2 * Window_Spare && h > Window_min_h + 2 * Window_Spare)
    {
        this->resize(w, h);
    }
}

void BaseMainWindow::SetUp()
{
    this->SetUI();
    this->SetUILocation();
    this->SetUIFun();
}
void BaseMainWindow::SetDay()
{
    LeftZoom->SetDay();
    Background->setStyleSheet("QLabel {"
                              "background-color: white;"
                              "border: 2px solid gray;"
                              "border-radius: 7px;"
                              "}");
    RightZoom->SetDay();
    LeftDownZoom->SetDay();
    DownZoom->SetDay();
    RightDownZoom->SetDay();

    MiniButton->setStyleSheet(WindowsButtonQss(WindowsButtonDayColor));
    CloseButton->setStyleSheet(WindowsButtonQss(WindowsButtonDayColor));
}
void BaseMainWindow::SetNight()
{
    LeftZoom->SetNight();
    // Background->SetNight();
    RightZoom->SetNight();
    LeftDownZoom->SetNight();
    DownZoom->SetNight();
    RightDownZoom->SetNight();
}

void BaseMainWindow::ShadowBig()
{
    // 创建动画
    QPropertyAnimation *animation = new QPropertyAnimation(BackgroundShadow, "blurRadius");
    animation->setDuration(200);                             // 动画持续时间
    animation->setStartValue(20);                            // 起始值
    animation->setEndValue(50);                              // 结束值
    animation->setEasingCurve(QEasingCurve::InOutQuad);      // 缓和曲线
    animation->start(QAbstractAnimation::DeleteWhenStopped); // 动画完成后删除对象
}

void BaseMainWindow::ShadowSmall()
{
    // 创建动画
    QPropertyAnimation *animation = new QPropertyAnimation(BackgroundShadow, "blurRadius");
    animation->setDuration(200);                             // 动画持续时间
    animation->setStartValue(50);                            // 起始值
    animation->setEndValue(20);                              // 结束值
    animation->setEasingCurve(QEasingCurve::InOutQuad);      // 缓和曲线
    animation->start(QAbstractAnimation::DeleteWhenStopped); // 动画完成后删除对象
}

BaseMainWindow::~BaseMainWindow()
{
}